package com.unitedats.common.util;

import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.unitedats.HR.Entity.HrDepartment;

public class HtmlUtil {

	public static String drawProjectTypeList(String name) {
		String html = "";
		for (Enums.PROJECT_TYPE type : Enums.PROJECT_TYPE.values())
			html += "<input	type='radio' name='" + name + "' value='" + type.getId() + "'>" + type.getName();
		return html;

	}

	public static String drawProjectStatusList(String name) {
		String html = "";
		for (Enums.PROJECT_STATUS status : Enums.PROJECT_STATUS.values())
			html += "<input	type='radio' name='" + name + "' value='" + status.getId() + "'>" + status.getName();
		return html;
	}

	public static String arrayFromJavaToJavaScript(String[] javaAry) {
		String jsAry = "";
		if (javaAry != null && javaAry.length > 0) {
			jsAry += "['" + javaAry[0] + "'";
			for (int i = 1; i < javaAry.length; i++)
				jsAry += ",'" + javaAry[i] + "'";
			jsAry += "]";
			return jsAry;
		}

		return "[]";
	}

	public static String arrayFromJavaToJavaScript(String concatenatedString) {
		return arrayFromJavaToJavaScript(Common.getConcatenatedValues(concatenatedString, false));
	}

	public static String drawDepartmentNodeTree(HrDepartment node, boolean updateMode, boolean root, DBManager dbm) {
		StringBuffer treeUL = new StringBuffer("");
		if (root)
			treeUL.append("<ul  class='orgChartList' style='display:none'>");

		treeUL.append("<li>");
		if (!updateMode) {
			treeUL.append(node.getName());
		} else {
			treeUL.append("<label>" + node.getName() + "</label>");
			treeUL.append("<input type='hidden' name='depId' value='" + node.getId() + "'/>");
			treeUL.append("<input type='text' name='depName' value='" + node.getName() + "'/>");
		}

		HrDepartment serachBy = new HrDepartment();
		serachBy.setParentId(node);
		serachBy.setStatus(true);
		List<HrDepartment> nodeChildren = dbm.find(serachBy);
		if (root)
			nodeChildren.remove(node);

		if (nodeChildren != null && !nodeChildren.isEmpty()) {
			treeUL.append("<ul>");
			for (HrDepartment child : nodeChildren)
				// if (child.getId() != 1)
				treeUL.append(drawDepartmentNodeTree(child, updateMode, false, dbm));
			treeUL.append("</ul>");
		} else {
			if (updateMode)
				treeUL.append("<img onclick='deleteOrgChartLeafNode(this)' id='" + node.getId()
						+ "' title='delete' src='image/delete.png' width='20' style='float:right' />");
		}

		treeUL.append("</li>");

		if (root)
			treeUL.append("</ul>");

		return treeUL.toString();
	}

	public static JSONArray getDepartmentListAsJSONArray(List<HrDepartment> depList) throws JSONException {
		JSONArray jsonArray = new JSONArray();
		for (HrDepartment dep : depList) {
			JSONObject jsonObj = new JSONObject();
			jsonObj.put("id", dep.getId());
			jsonObj.put("name", dep.getName());
			jsonArray.put(jsonObj);
		}
		return jsonArray;
	}
}
